package com.chan.web.controller.cd;

import java.util.List;

import com.chan.common.annotation.Log;
import com.chan.common.core.controller.BaseController;
import com.chan.common.core.domain.AjaxResult;
import com.chan.common.core.page.TableDataInfo;
import com.chan.common.enums.BusinessType;
import com.chan.common.utils.poi.ExcelUtil;
import com.chan.domain.CdBatchFile;
import com.chan.domain.CdProgramFile;
import com.chan.service.ICdProgramFileService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chan.domain.CdProgramItem;
import com.chan.service.ICdProgramItemService;


/**
 * 功能Controller
 *
 * @author chan
 * @date 2024-04-30
 */
@Controller
@RequestMapping("/cd/programItem")
public class CdProgramItemController extends BaseController
{
    private String prefix = "cd/programItem";

    @Autowired
    private ICdProgramItemService cdProgramItemService;
    @Autowired
    private ICdProgramFileService cdProgramFileService;
    @RequiresPermissions("cd:programItem:view")
    @GetMapping()
    public String programItem()
    {
        return prefix + "/programItem";
    }

    /**
     * 查询功能列表
     */
    @RequiresPermissions("cd:programItem:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CdProgramItem cdProgramItem)
    {
        startPage();
        List<CdProgramItem> list = cdProgramItemService.selectCdProgramItemList(cdProgramItem);
        return getDataTable(list);
    }

    /**
     * 导出功能列表
     */
    @RequiresPermissions("cd:programItem:export")
    @Log(title = "功能", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CdProgramItem cdProgramItem)
    {
        List<CdProgramItem> list = cdProgramItemService.selectCdProgramItemList(cdProgramItem);
        ExcelUtil<CdProgramItem> util = new ExcelUtil<CdProgramItem>(CdProgramItem.class);
        return util.exportExcel(list, "功能数据");
    }

    /**
     * 新增功能
     */
    @GetMapping("/add/{relateId}")
    public String add(@PathVariable("relateId") String relateId,ModelMap mmap){
        System.out.println(relateId);
        mmap.put("relateId", relateId);
        return prefix + "/add";
    }
    @GetMapping("/upload")
    public String upload(String relateId,String relateMode,ModelMap mmap){
        System.out.println(relateId);
        System.out.println(relateMode);
        mmap.put("relateId", relateId);
        mmap.put("relateMode", relateMode);
        return prefix + "/upload";
    }
    @PostMapping("/batchAdd")
    @ResponseBody
    public AjaxResult batchAddSave(CdBatchFile cdBatchFile)
    {
        return toAjax(cdProgramFileService.batchInsertCdProgramFile(cdBatchFile));
    }
    /**
     * 新增保存功能
     */
    @RequiresPermissions("cd:programItem:add")
    @Log(title = "功能", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CdProgramItem cdProgramItem)
    {
        return toAjax(cdProgramItemService.insertCdProgramItem(cdProgramItem));
    }

    /**
     * 修改功能
     */
    @RequiresPermissions("cd:programItem:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CdProgramItem cdProgramItem = cdProgramItemService.selectCdProgramItemById(id);
        mmap.put("cdProgramItem", cdProgramItem);
        return prefix + "/edit";
    }

    /**
     * 修改保存功能
     */
    @RequiresPermissions("cd:programItem:edit")
    @Log(title = "功能", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CdProgramItem cdProgramItem)
    {
        return toAjax(cdProgramItemService.updateCdProgramItem(cdProgramItem));
    }

    /**
     * 删除功能
     */
    @RequiresPermissions("cd:programItem:remove")
    @Log(title = "功能", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cdProgramItemService.deleteCdProgramItemByIds(ids));
    }
}
